package com.sinodata.bsm.inmp.finder.gab;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.inmp.finder.Finder;
import com.sinodata.bsm.inmp.util.DBConnectionUtil;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-9-11 下午4:15:52          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class AnjianFinder extends Finder {

    private static final Logger logger = Logger.getLogger(AnjianFinder.class);

    @SuppressWarnings("rawtypes")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String deviceType = (String) params.get("DEVICETYPE");
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        connection = DBConnectionUtil.getInstance().getanjianConnection();
        try {
            statement = connection.createStatement();
            set = statement.executeQuery(getSql(deviceType));
            while (set.next()) {
                String[] res = new String[7];
                list.add(res);
                res[0] = "anjian_" + set.getString(1);//内部标示id
                res[1] = set.getString(2) + "_" + set.getString(3);
                res[2] = set.getString(2) + "_" + set.getString(3) + "_" + set.getString(4);
                res[3] = set.getString(2);
                res[4] = set.getString(3);
                res[5] = set.getString(4);
                res[6] = set.getString(5);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                DBConnectionUtil.getInstance().returnConn(connection);
            }
        }
        return list;
    }

    private String getSql(String deviceType) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("select ");
        buffer.append("id, ");
        buffer.append("name, ");
        buffer.append("ip, ");
        buffer.append("device_type, ");
        buffer.append("company ");
        buffer.append("from ");
        buffer.append("v_cms_device ");
        buffer.append("where ");
        buffer.append("device_type = '");
        buffer.append(deviceType);
        buffer.append("' ");
        buffer.append("order by id ");
        String sql = buffer.toString();
        logger.info("AnjianFinder.getSql>>sql[" + sql + "]");
        return sql;
    }
}
